/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

Ext.QuickTips.init();
var _pageSize = 20;
var _title = '月统计表';
var store;
var now = new Date();
var loadServlet = basePath+'servlet/data/YsjLoad';

var tabs = new Ext.TabPanel({
    activeTab: 0,
    plain: true,
    enableTabScroll: true,
    defaults: {autoScroll: true},
    items: [{
            title: currentOrgName + _title,
            id: 'main-tab',
            layout: "fit",
            tbar: new Ext.Toolbar([
                {
                    text: '下级单位明细',
                    iconCls: 'icon-daytime',
                    handler: showSubOrg
                            , style: {marginLeft: '10px'}
                },
                {
                    text: '环比数据',
                    iconCls: 'icon-linkratio',
                    handler: compareRangeData
                },
                {
                    text: '合格率环比',
                    iconCls: 'icon-cycleratio',
                    handler: compareRange
                },
                {
                    text: '合格率同比',
                    iconCls: 'icon-linkratio',
                    handler: compareSame
                }, '-', yearToCombo, lblYear, monthToCombo, lblMonth,
                new tlf.component.RangeCombo({
                    listeners: {
                        select: function(combo, record, index) {
                            store.baseParams.range = combo.getValue();
                        }
                    }
                }),
                new tlf.component.ReportStatusCombo({
                    listeners: {
                        select: function(combo, record, index) {
                            store.baseParams.reportStatus = combo.getValue();
                        }
                    }
                }),
                searchButton
            ])
        }
    ]
});

var hourSeries = [
    {
        yField: 'voltage',
        displayName: '电压值',
        style: {
            fillColor: 0xAAAAFF,
            borderColor: 0x3333FF,
            lineColor: 0x3333FF
        }
    }
];
var extraStyle = {
    xAxis: {
        majorGridLines: {color: 0x999999, size: 1}
    },
    yAxis: {
        titleRotation: -90
    },
    legend: {
        display: "right",
        padding: 5,
        spacing: 2,
        font: {color: 0x000000, family: "Arial", size: 12},
        border: {size: 1, color: 0x999999}
    }

};
function showSubOrg() {
    //alert(record.get('name'));
    var vid = '下级单位明细';

    var storeSub = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: tlf.data.fieldNameRsjOrg,
        baseParams: {lock:lock,cmd: 'ysjSubOrg', yearTo: now.format('Y'), monthTo: now.format("m")},
        autoLoad: true
    });

    var yearToCombo = new tlf.component.YearCombo({
        value: thisYear,
        listeners: {
            select: function(combo, record, index) {
                storeSub.baseParams.yearTo = this.getValue();
            }
        }
    });

    var monthToCombo = new tlf.component.MonthCombo({
        value:thisMonth,
        listeners: {
            select: function(combo, record, index) {
                storeSub.baseParams.monthTo = this.getValue();
            }
        }        
    });
    var gridSub = new Ext.grid.GridPanel({
        store: storeSub,
        columns: [
            checkboxSel,
            new Ext.grid.RowNumberer({width: 40, header: '序号'})
        ].concat(tlf.data.columnsOrgRsj),
        loadMask: true
    });

    var tab = tabs.add({
        title: vid,
        closable: true,
        id: vid,
        layout: 'fit',
        tbar: [
            yearToCombo, lblYear,
            monthToCombo, lblMonth,
            new tlf.component.RangeCombo({
                    listeners: {
                        select: function(combo, record, index) {
                            storeSub.baseParams.range = combo.getValue();
                        }
                    }
                }),
                new tlf.component.ReportStatusCombo({
                    listeners: {
                        select: function(combo, record, index) {
                            storeSub.baseParams.reportStatus = combo.getValue();
                        }
                    }
                }),
            {
                xtype: 'button',
                text: '搜索',
                iconCls: 'icon-search',
                width: 60,
                style: {marginLeft: '20px'},
                handler: function() {
                    storeSub.reload();
                }
            }
        ]
                , items: [gridSub]
    });
    tab.show();
}

//某个单位多日日数据显示
function compareRangeData() {

    var vid = currentOrgName + "月数据环比";
    var ysjRangeDataOrgStore = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: tlf.data.fieldNameRsjOrg,
        baseParams: {lock:lock,cmd: 'ysjRangeOrg', yearFrom:now.add(Date.MONTH,-12).format('Y'),monthFrom:now.format('m'),yearTo:now.format('Y'),monthTo:now.format('m'), start: 0, limit: _pageSize},
        autoLoad: true
    });

    var gridRangeDataOrg = new Ext.grid.GridPanel({
        store: ysjRangeDataOrgStore,
        columns: [
            checkboxSel,
            new Ext.grid.RowNumberer({width: 40, header: '序号'})
        ].concat(tlf.data.columnsOrgRsj),
        loadMask: true,
        bbar: new Ext.PagingToolbar({
            pageSize: _pageSize,
            store: ysjRangeDataOrgStore,
            displayInfo: true,
            displayMsg: '显示记录：{0} - {1}   总记录：{2}',
            emptyMsg: '目前没有信息'
        })
    });

    var yearFromCombo = new tlf.component.YearCombo({
        value: thisYear-1,
        listeners: {
            select: function(combo, record, index) {
                ysjRangeDataOrgStore.baseParams.yearFrom = this.getValue();
            }
        }
    });

    var monthFromCombo = new tlf.component.MonthCombo({
        value:thisMonth,
        listeners: {
            select: function(combo, record, index) {
                ysjRangeDataOrgStore.baseParams.monthFrom = this.getValue();
            }
        }        
    });
    var yearToCombo = new tlf.component.YearCombo({
        value: thisYear,
        listeners: {
            select: function(combo, record, index) {
                ysjRangeDataOrgStore.baseParams.yearTo = this.getValue();
            }
        }
    });

    var monthToCombo = new tlf.component.MonthCombo({
        value:thisMonth,
        listeners: {
            select: function(combo, record, index) {
                ysjRangeDataOrgStore.baseParams.monthTo = this.getValue();
            }
        }        
    });
    
    var tab = tabs.add({
        title: vid,
        tbar: [
            yearFromCombo,lblYear,monthFromCombo,lblMonth,lblTo,yearToCombo,lblYear,monthToCombo,lblMonth ,
            new tlf.component.RangeCombo({
                    listeners: {
                        select: function(combo, record, index) {
                            ysjRangeDataOrgStore.baseParams.range = combo.getValue();
                        }
                    }
                }),
                new tlf.component.ReportStatusCombo({
                    listeners: {
                        select: function(combo, record, index) {
                            ysjRangeDataOrgStore.baseParams.reportStatus = combo.getValue();
                        }
                    }
                }),
            {
                xtype: 'button',
                text: '搜索',
                iconCls: 'icon-search',
                width: 60,
                style: {marginLeft: '20px'},
                handler: function() {
                    ysjRangeDataOrgStore.reload();
                }
            }],
        closable: true,
        id: vid,
        layout: 'fit'
    });
    Ext.getCmp(vid).add(gridRangeDataOrg);
    tab.show();
}


function compareSame() {
    var vid = currentOrgName + " 日合格率同比";
    var tipRenderer = function(chart, rec, index, series) {
        var yearInfo = '日期: ' + rec.get('dataDate');
        var empInfo = '合格率' + ': '
                + rec.get('rateTotal');
        return yearInfo + '\n' + empInfo;
    };
    var extraStyle = {
        xAxis: {
            majorGridLines: {
                color: 0x999999,
                size: 1
            }
        },
        yAxis: {
            titleRotation: -90
        }
    };
    var ysjOrgSameStore = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'dataDate',
        totalProperty: 'count',
        autoLoad: true,
        fields: ['dataDate', 'rateTotal'],
        baseParams: {lock:lock,cmd: "ysjOrgSame", yearFrom: now.add(Date.YEAR, -1).format('Y'),yearTo: now.format('Y'), monthTo: now.format('m')}
    });    
    var chartPanel = {
        xtype: 'columnchart',
        store: ysjOrgSameStore,
        xField: 'dataDate',
        tipRenderer: tipRenderer,
        yField: 'rateTotal',
        extraStyle: extraStyle,
        xAxis: new Ext.chart.CategoryAxis({
            displayName: '日期',
            title: '日期'
        }),
        yAxis: new Ext.chart.NumericAxis({
            displayName: '电压合格率',
            title: '电压合格率',
            labelRenderer: Ext.util.Format.
                    numberRenderer('0,0.000')
        })
    };


    var yearFromCombo = new tlf.component.YearCombo({
        value: thisYear-1,
        listeners: {
            select: function(combo, record, index) {
                ysjOrgSameStore.baseParams.yearFrom = this.getValue();
            }
        }
    });
    
    var yearToCombo = new tlf.component.YearCombo({
        value: thisYear,
        listeners: {
            select: function(combo, record, index) {
                ysjOrgSameStore.baseParams.yearTo = this.getValue();
            }
        }
    });

    var monthToCombo = new tlf.component.MonthCombo({
        value:thisMonth,
        listeners: {
            select: function(combo, record, index) {
                ysjOrgSameStore.baseParams.monthTo = this.getValue();
            }
        }        
    });
    var tab = tabs.add({
        title: vid,
        //iconCls: 'icon-tab',
        //bodyStyle: 'padding: 5px',
        //html:'abc',
        tbar: [
            yearFromCombo,lblYear,lblTo,yearToCombo,lblYear,monthToCombo,lblMonth ,
            new tlf.component.RangeCombo({
                    listeners: {
                        select: function(combo, record, index) {
                            ysjOrgSameStore.baseParams.range = combo.getValue();
                        }
                    }
                }),
                new tlf.component.ReportStatusCombo({
                    listeners: {
                        select: function(combo, record, index) {
                            ysjOrgSameStore.baseParams.reportStatus = combo.getValue();
                        }
                    }
                }),
            {
                xtype: 'button',
                text: '搜索',
                iconCls: 'icon-search',
                width: 60,
                style: {marginLeft: '20px'},
                handler: function() {
                    ysjOrgSameStore.reload();
                }
            }],
        closable: true,
        id: vid,
        layout: 'fit'
    });
    //tab.add(chartPanel);
    Ext.getCmp(vid).add(chartPanel);
    //alert('cde');
    tab.show();
}

function compareRange() {
    var vid = currentOrgName + ' 月合格率曲线';

    var tipRenderer = function(chart, rec, index, series) {
        var yearInfo = '日期: ' + rec.get('dataDate');
        var empInfo = '合格率' + ': '
                + rec.get('rateTotal');
        return yearInfo + '\n' + empInfo;
    };
    var extraStyle = {
        xAxis: {
            majorGridLines: {
                color: 0x999999,
                size: 1
            }
        },
        yAxis: {
            titleRotation: -90
        }
    };
    var ysjRangeDataOrgStore = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: tlf.data.fieldNameRsjOrg,
         baseParams: {lock:lock,cmd: 'ysjRangeOrg', yearFrom:now.add(Date.MONTH,-12).format('Y'),monthFrom:now.format('m'),yearTo:now.format('Y'),monthTo:now.format('m'), start: 0, limit: _pageSize},
        autoLoad: true
    });
    
    var chartPanel = {
        xtype: 'linechart',
        store: ysjRangeDataOrgStore,
        xField: 'dataDate',
        tipRenderer: tipRenderer,
        yField: 'rateTotal',
        extraStyle: extraStyle,
        xAxis: new Ext.chart.CategoryAxis({
            displayName: '日期',
            title: '日期'
        }),
        yAxis: new Ext.chart.NumericAxis({
            displayName: '电压合格率',
            title: '电压合格率',
            labelRenderer: Ext.util.Format.
                    numberRenderer('0,0.000')
        })
    };

    var yearFromCombo = new tlf.component.YearCombo({
        value: thisYear-1,
        listeners: {
            select: function(combo, record, index) {
                ysjRangeDataOrgStore.baseParams.yearFrom = this.getValue();
            }
        }
    });

    var monthFromCombo = new tlf.component.MonthCombo({
        value:thisMonth,
        listeners: {
            select: function(combo, record, index) {
                ysjRangeDataOrgStore.baseParams.monthFrom = this.getValue();
            }
        }        
    });
    var yearToCombo = new tlf.component.YearCombo({
        value: thisYear,
        listeners: {
            select: function(combo, record, index) {
                ysjRangeDataOrgStore.baseParams.yearTo = this.getValue();
            }
        }
    });

    var monthToCombo = new tlf.component.MonthCombo({
        value:thisMonth,
        listeners: {
            select: function(combo, record, index) {
                ysjRangeDataOrgStore.baseParams.monthTo = this.getValue();
            }
        }        
    });
    var tab = tabs.add({
        title: vid,
        tbar: [
            yearFromCombo,lblYear,monthFromCombo,lblMonth,lblTo,yearToCombo,lblYear,monthToCombo,lblMonth ,
            new tlf.component.RangeCombo({
                    listeners: {
                        select: function(combo, record, index) {
                            ysjRangeDataOrgStore.baseParams.range = combo.getValue();
                        }
                    }
                }),
                new tlf.component.ReportStatusCombo({
                    listeners: {
                        select: function(combo, record, index) {
                            ysjRangeDataOrgStore.baseParams.reportStatus = combo.getValue();
                        }
                    }
                }),
            {
                xtype: 'button',
                text: '刷新',
                iconCls: 'icon-refresh',
                width: 60,
                style: {marginLeft: '20px'},
                handler: function() {
                    ysjRangeDataOrgStore.reload();
                }
            }],
        closable: true,
        id: vid,
        layout: 'fit'
    });
    Ext.getCmp(vid).add(chartPanel);
    tab.show();
}


var checkboxSel = new Ext.grid.CheckboxSelectionModel();
function buildWindow() {
    store = new Ext.data.JsonStore({
        url: loadServlet,
        root: 'data',
        idProperty: 'id',
        totalProperty: 'count',
        fields: tlf.data.fieldNameRsjOrg,
        autoLoad: true,
        baseParams: {lock:lock,cmd: 'ysjOrg', yearTo: now.format('Y'), monthTo: now.format("m"), start: 0, limit: _pageSize}
    });

    var grid = new Ext.grid.GridPanel({
        store: store,
        //sm: checkboxSel,
        columns:[
            checkboxSel,
            new Ext.grid.RowNumberer({width: 40, header: '序号'})
        ].concat(tlf.data.columnsOrgRsj),
        loadMask: true,
        bbar: new Ext.PagingToolbar({
            pageSize: _pageSize,
            store: store,
            displayInfo: true,
            displayMsg: '显示记录：{0} - {1}   总记录：{2}',
            emptyMsg: '目前没有信息'
        })
    });

    Ext.getCmp('main-tab').add(grid);
    var applicationUI = new Ext.Viewport({
        layout: "fit",
        items: [
            tabs
        ]
    });
}
Ext.onReady(buildWindow);
